package com.dxl.system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxl.base.Result;
import com.dxl.base.Results;
import com.dxl.system.mapper.NoteMapper;
import com.dxl.system.mapper.SeriesMapper;
import com.dxl.system.model.Note;
import com.dxl.system.model.Series;
import com.dxl.system.service.NoteService;
import com.dxl.system.service.SeriesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <b> 系列服务接口实现类
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-09-25    <br>Author : dxl
 */
@Service
public class SeriesServiceImpl extends ServiceImpl<SeriesMapper,Series> implements SeriesService {

    @Autowired(required = false)
    private SeriesMapper seriesMapper;
    @Autowired(required = false)
    private NoteMapper noteMapper;
    @Autowired(required = false)
    private NoteService noteService;

    /**
     * <b> 插入系列
     * </b><br><br><i>Description</i> : 不能为空：系列名称-series_name ；不能重复：系列名称-series_name  
     * @param series Series
     * @return com.dxl.base.Result Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result addOne(Series series) {
        Result result = isSeriesAddable(series);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        seriesMapper.insert(series);
        return Results.successWithData(series);
    }

    /**
     * <b> 删除系列
     * </b><br><br><i>Description</i> : 
     * @param seriesIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result delByIds(List<Integer> seriesIds) {
        int num = seriesMapper.deleteBatchIds(seriesIds);

        return Results.successWithData(num);
    }


    /**
     * <b> 编辑系列
     * </b><br><br><i>Description</i> : 不能重复：系列名称-series_name 
     * @param series Series
     * @return com.dxl.base.Result Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result editOne(Series series) {
        Result result = isSeriesEditable(series);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        seriesMapper.updateById(series);
        return Results.successWithData(series);
    }

    /**
     * <b> ID查询系列
     * </b><br><br><i>Description</i> : 
     * @param seriesId Integer
     * @return com.dxl.base.Result: Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getById(Integer seriesId) {
        Series series = seriesMapper.selectById(seriesId);
        return Results.successWithData(series);
    }

    /**
     * <b> 条件查询系列
     * </b><br><br><i>Description</i> : 
     * @param series Series
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getList(Series series) {
        QueryWrapper<Series> seriesWrapper = new QueryWrapper<Series>(series);
        seriesWrapper.orderByDesc("id");
        List<Series> seriesList = seriesMapper.selectList(seriesWrapper);
        seriesList = colorSeries(seriesList);
        return Results.successWithData(seriesList);
    }

    /**
     * <b> 分页条件查询系列
     * </b><br><br><i>Description</i> : 
     * @param series Series, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result getListByPage(Series series, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Series> seriesList = (List<Series>) getList(series).getData();
        PageInfo<Series> pageInfo = new PageInfo<Series>(seriesList);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 模糊查询系列
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：series_name-系列名称 
     * @param series Series
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getLikeSeries(Series series) {
        QueryWrapper<Series> seriesWrapper = new QueryWrapper<>();
        seriesWrapper.orderByDesc("id");
        if(series.getSeriesName() != null){
            seriesWrapper.like("series_name",series.getSeriesName());
        }else{
            return Results.failure("请正确填写属性:(");
        }
        List<Series> seriesList = seriesMapper.selectList(seriesWrapper);
        seriesList = colorSeries(seriesList);
        return Results.successWithData(seriesList);
    }
    /**
     * <b> 分页模糊查询系列
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：series_name-系列名称 
     * @param series Series, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result getLikeSeriesByPage(Series series, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Series> seriesList = (List<Series>) getLikeSeries(series).getData();
        PageInfo<Series> pageInfo = new PageInfo<Series>(seriesList);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 系列能否新增
     * </b><br><br><i>Description</i> : 不能为空：系列名称-series_name ；不能重复：系列名称-series_name  
     * @param series Series
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result isSeriesAddable(Series series) {

        if(series.getSeriesName() == null){
            return Results.successWithData(false,"系列名称 不能为空:(");
        }

        if(series.getSeriesName() != null){
        QueryWrapper<Series> seriesWrapper = new QueryWrapper<>();
        seriesWrapper.eq("series_name",series.getSeriesName());
        List<Series> seriesList = seriesMapper.selectList(seriesWrapper);
            if(seriesList.size() > 0){
                return Results.successWithData(false,"系列名称 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }

    /**
     * <b> 系列能否编辑
     * </b><br><br><i>Description</i> : 不能重复：系列名称-series_name 
     * @param series Series
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result isSeriesEditable(Series series) {

        if(series.getSeriesName() != null){
            QueryWrapper<Series> seriesWrapper1 = new QueryWrapper<>();
            seriesWrapper1.eq("id",series.getId()).eq("series_name",series.getSeriesName());
            List<Series> seriesList1 = seriesMapper.selectList(seriesWrapper1);
            QueryWrapper<Series> seriesWrapper2 = new QueryWrapper<>();
            seriesWrapper2.eq("series_name",series.getSeriesName());
            List<Series> seriesList2 = seriesMapper.selectList(seriesWrapper2);
            if(!((seriesList1.size() == 1 && seriesList2.size() == 1) ||(seriesList1.size() == 0 && seriesList2.size() == 0))){
                return Results.successWithData(false,"系列名称 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }

    /**
     * <b> 分页条件查询系列中文件
     * </b><br><br><i>Description</i> :
     * @param series Series, currentPage -Integer
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result getAllNoteInSeriesByNameByPage(Series series, Integer currentPage, Integer pageSize) {
        QueryWrapper<Series> seriesQueryWrapper = new QueryWrapper<>();
        seriesQueryWrapper.eq("series_name",series.getSeriesName().replace("[","").replace("]",""));
        Series series1 = seriesMapper.selectList(seriesQueryWrapper).get(0);
        PageHelper.startPage(currentPage, pageSize);
        QueryWrapper<Note> noteQueryWrapper = new QueryWrapper<>();
        noteQueryWrapper.eq("note.series_id",series1.getId());
        List<Note> noteList = noteMapper.getJoinLabelSeries(noteQueryWrapper);
        noteList = noteService.handleNote(noteList);
        PageInfo<Note> pageInfo = new PageInfo<Note>(noteList);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 给系列字体、边框随机颜色
     * </b><br><br><i>Description</i> :
     * @param seriesList
     * @return java.util.List<com.dxl.system.model.Label>
     * <br><br>Date: 2019/9/29 11:00     <br>Author : dxl
     */
    private List<Series> colorSeries(List<Series> seriesList){
        List<String> fontColorList = new ArrayList<>();
        fontColorList.add("#6f42c1");
        fontColorList.add("#0f0f0f");
        fontColorList.add("#1b6d85");
        fontColorList.add("#002b36");
        fontColorList.add("#00B83F");
        fontColorList.add("#2b542c");
        fontColorList.add("#122b40");
        fontColorList.add("#761c19");
        fontColorList.add("#8a6d3b");
        fontColorList.add("#A705ED");
        fontColorList.add("#cb4b16");
        fontColorList.add("blue");
        fontColorList.add("maroon");
        fontColorList.add("#24292e");
        fontColorList.add("dodgerblue");
        String[] char16 = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
        for(Series series : seriesList){
            series.setSeriesFontColor(fontColorList.get((int)(1+Math.random()*13)));
            StringBuffer sb = new StringBuffer();
            sb.append("#");
            for(int i = 0; i < 6; i++){
                sb.append(char16[(int)(1+Math.random()*15)]);
            }
            series.setSeriesSolidColor(sb.toString());
        }
        return seriesList;
    }
}
